package leetcode.editor.cn;

//将非负整数 num 转换为其对应的英文表示。 
//
// 
//
// 示例 1： 
//
// 
//输入：num = 123
//输出："One Hundred Twenty Three"
// 
//
// 示例 2： 
//
// 
//输入：num = 12345
//输出："Twelve Thousand Three Hundred Forty Five"
// 
//
// 示例 3： 
//
// 
//输入：num = 1234567
//输出："One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
// 
//
// 示例 4： 
//
// 
//输入：num = 1234567891
//输出："One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven 
//Thousand Eight Hundred Ninety One"
// 
//
// 
//
// 提示： 
//
// 
// 0 <= num <= 2³¹ - 1 
// 
// Related Topics 递归 数学 字符串 👍 244 👎 0

import java.util.HashMap;
import java.util.Map;

//Java：整数转换英文表示
public class IntegerToEnglishWords_273{
    public static void main(String[] args) {
        Solution solution = new IntegerToEnglishWords_273().new Solution();
        // TO TEST
    }
    
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        String[] percentile = {"","Thousand","Million","Billion"}; //百分位数
        String[] numStr={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
        String[] numteen={"Ten", "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
        String[] numty={"","", "Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    public String numberToWords(int num) {
        if(num==0) return numStr[0];
        StringBuffer res = new StringBuffer();

        for (int i = 0; i < 4; i++) {
            if(num==0) break;
            String s =numToWords(num%1000, percentile[i]);
            num /=1000;
            if(s.equals("")) continue; //1,000,111
            if(res.length() !=0) s=s+" ";
            System.out.println(percentile[i]+":["+s+"]");
            res.insert(0,s);
        }
        return res.toString();
    }

    public String numToWords(int num, String str){
        if (num==0) return "";
        StringBuffer sb = new StringBuffer();
        if(num/100!=0){ // 百位
            sb.append(numStr[num/100]).append(" ").append("Hundred");
        }
        num%=100;
        if(num/10!=0){ //十位
            if(sb.length()!=0) sb.append(" ");
            int n = num/10;
            if (n==1){ // 10-19
                sb.append(numteen[num%10]);
                num=0;
            }else{ //20,30,...,90
                sb.append(numty[n]);
            }
        }
        num%=10;
        if(num!=0){
            if(sb.length()!=0) sb.append(" ");
            sb.append(numStr[num]); // 个位
        }
        if(str.length()!=0) sb.append(" ");
        sb.append(str);
        return sb.toString();
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
